package com.cgwx.systemservice.controller;


import com.cgwx.commonutils.result.SysResult;
import com.cgwx.springsecurity.annotation.Log;
import com.cgwx.springsecurity.enums.BusinessType;
import com.cgwx.systemservice.entity.sys.SysMenu;
import com.cgwx.systemservice.entity.vo.AssginMenuVo;
import com.cgwx.systemservice.service.SysRoleMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色菜单 前端控制器
 * </p>
 *
 * @author GaoWei
 * @since 2023-10-12
 */

@Api(tags = "角色菜单前端控制器")
@RestController
@RequestMapping("/system/sysRoleMenu")
@CrossOrigin
public class SysRoleMenuController {

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    //根据角色分配菜单
    @ApiOperation("根据角色获取菜单")
    @GetMapping("/toAssign/{roleId}")
    public SysResult toAssign(
            @ApiParam(name = "roleId", value = "角色ID")
            @PathVariable Long roleId) {
        List<SysMenu> list = sysRoleMenuService.findMenuByRoleId(roleId);
        return SysResult.ok(list);
    }

    @Log(title = "给角色分配菜单权限", businessType = BusinessType.ASSGIN)
    @PreAuthorize("hasAuthority('bnt.sysRole.assignAuth')")
    @ApiOperation("给角色分配菜单权限")
    @PostMapping("/doAssign")
    public SysResult doAssign(
            @ApiParam(name = "assginMenuVo", value = "给角色id分配菜单id的VO类")
            @RequestBody AssginMenuVo assginMenuVo) {
        sysRoleMenuService.doAssign(assginMenuVo);
        return SysResult.ok();
    }
}

